﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CommonManage = DevelopStudio.Common.Manage;
using CommonBasic = DevelopStudio.Common.Basic;
using System.IO;
using System.Diagnostics;
using SNSHelper.Common;

namespace DevelopDiff
{
    public partial class DiffMainForm : Form
    {
        private string[] arguments;

        private CommonBasic.DSFileDiffInfo diffInfo;

        public DiffMainForm(string[] args)
        {
            InitializeComponent();
            arguments = args;
        }

        private void DiffMainForm_Load(object sender, EventArgs e)
        {
            this.Reload();
        }

        private void Reload()
        {
            if (arguments.Length <= 0)
            {
                this.labelPath.Text = Properties.Resources.ErrorFilePath;

                CommonManage.Utility.ShowMessage(this.labelPath, Properties.Resources.ErrorFilePath, CommonBasic.MessageLevel.Critical);
                return;
            }

            string path = arguments[0];

            if (File.Exists(path) == false)
            {
                CommonManage.Utility.ShowMessage(this.labelPath, Properties.Resources.ErrorFilePath, CommonBasic.MessageLevel.Critical);
                return;
            }

            FileInfo fileInfo = new FileInfo(path);

            if (fileInfo.Extension != CommonBasic.AppConst.DiffToolFileSuffix)
            {
                CommonManage.Utility.ShowMessage(this.labelPath, Properties.Resources.ErrorFilePath, CommonBasic.MessageLevel.Critical);
                return;
            }

            diffInfo = CommonBasic.DSFileDiffInfo.ParseToInstance(path);

            if (diffInfo == null)
            {
                CommonManage.Utility.ShowMessage(this.labelPath, Properties.Resources.ErrorFilePath, CommonBasic.MessageLevel.Critical);
                return;
            }

            CommonManage.Utility.ShowMessage(this.labelPath, path, CommonBasic.MessageLevel.Normal);

            int count = 0;
            this.listView1.Items.Clear();
            foreach (CommonBasic.DSFileDiffUnitInfo var in diffInfo.DiffInfos)
            {
                ListViewItem item = new ListViewItem(new string[] {
                var.DisplayPath.Replace(var.WorkingCopyPath, ""),
                Path.GetExtension(var.DisplayPath),
                CommonManage.Utility.StatusDisplayName((CommonBasic.SvnStatus)var.Status),
                var.Left.Revision < 0 ? string.Empty : var.Left.Revision.ToString(),
                var.Right.Revision <= 0 ? string.Empty : var.Right.Revision.ToString()
                });
                item.Tag = var;

                if (var.Status == (int)CommonBasic.SvnStatus.Deleted)
                {
                    item.BackColor = System.Drawing.Color.Red;
                }
                else if (count % 2 == 0)
                {
                    item.BackColor = System.Drawing.SystemColors.ScrollBar;
                }

                this.listView1.Items.Add(item);

                ++count;
            }

            if (this.listView1.Items.Count > 0)
            {
                this.listView1.Items[0].Selected = true;
            }
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            this.Reload();
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            foreach (ListViewItem var in this.listView1.SelectedItems)
            {
                this.OpenDiffFiles(var);
                break;
            }
        }

        private void OpenDiffFiles(ListViewItem var)
        {
            CommonBasic.DSFileDiffUnitInfo unitDiffInfo = var.Tag as CommonBasic.DSFileDiffUnitInfo;

            if (unitDiffInfo == null)
            {
                return;
            }

            string param;
            if (diffInfo.LocalDiff == true)
            {
                param = @"/command:diff /path:" + unitDiffInfo.DisplayPath;
            }
            else
            {
                if (unitDiffInfo.UnPack(this.labelPath.Text) == false)
                {
                    MessageBox.Show(Properties.Resources.ErrorFormat);
                    return;
                }

                param = @"/command:diff";

                if (string.IsNullOrEmpty(unitDiffInfo.Right.Path) == false)
                {
                    param += " /path:" + unitDiffInfo.Right.Path;
                }

                if (string.IsNullOrEmpty(unitDiffInfo.Left.Path) == false)
                {
                    param += @" /path2:" + unitDiffInfo.Left.Path;
                }
            }

            param += "  /closeonend:0";

            Process.Start("TortoiseProc.exe", param);
        }

        private void listView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                foreach (ListViewItem var in this.listView1.SelectedItems)
                {
                    this.OpenDiffFiles(var);
                    break;
                }
            }
        }

        private void DiffMainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                DirectoryInfo folderInfo = new DirectoryInfo(CommonManage.Utility.TempDiffPath);

                foreach (FileInfo fileInfo in folderInfo.GetFiles())
                {
                    try
                    {
                        fileInfo.Delete();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception)
            {
            }

            if (Directory.Exists(CommonManage.Utility.TempDiffPath) == false)
            {
                Directory.CreateDirectory(CommonManage.Utility.TempDiffPath);
            }
        }
    }
}
